﻿Partial Public Class TextDialog

    ''' <summary>
    ''' Holds a value indicating whether the user is required to enter some text
    ''' </summary>
    ''' <remarks></remarks>
    Private _isRequired As Boolean

    ''' <summary>
    ''' Creates a new instance of the TextDialog
    ''' </summary>
    ''' <param name="prompt">This is the question to ask the user such as "New Script Name"</param>
    ''' <param name="title">This is the text that appears at the top of the dialog.</param>
    ''' <param name="isRequired">If true, the user will not be permitted to click OK</param>
    ''' <param name="defaultValue">This is the default value that will appear in the textbox when the dialog is displayed.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal prompt As String, _
                   ByVal title As String, _
                   ByVal isRequired As Boolean, _
                   ByVal defaultValue As String)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        PromptTextBlock.Text = prompt

        'Set the tile
        Me.TitleTextBlock.Text = title

        'Save this for later - we will need it to enable the buttons
        _isRequired = isRequired

        'Set the default value
        Me.ValueTextBox.Text = defaultValue

        'Select all of the text in the textbox
        Me.ValueTextBox.SelectAll()

        'Focus on the textbox
        Me.ValueTextBox.Focus()

        'Set up the UI state
        EnableButtons()
    End Sub

    ''' <summary>
    ''' Updates the UI
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableButtons()
        OKButton.IsEnabled = Not String.IsNullOrEmpty(Me.UserText) OrElse Not _isRequired
    End Sub

    ''' <summary>
    ''' Gets the text the user entered
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public ReadOnly Property UserText() As String
        Get
            Return Me.ValueTextBox.Text
        End Get
    End Property

    Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.DialogResult = True
        Me.Close()
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.DialogResult = False
        Me.Close()
    End Sub

    ''' <summary>
    ''' The OK button enabled status changes according to whether the user enters text or not.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ValueTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
        EnableButtons()
    End Sub

    Private Sub topBar_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Me.DragMove()
    End Sub
End Class
